import com.vhly.util.DeviceInfo;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Spore extends MIDlet
        implements CommandListener {

    static Display disp;
    static MainThread mainThreadImpl;
    Thread myThread;
    private SplashScreen splashScreen;
    public static boolean d;
    public static boolean e;
    public static boolean f;
    public static String spyURL = "http://go.i139.cn/gcomm1/portal/spchannel.do?url=http://gamepie.i139.cn/wap/s.do?j=3channel";
    public static boolean h = false;
    public Image exitImage;
    Spore outInstance;
    public static boolean l;
    private boolean fld_n_boolean;
    private boolean fld_o_boolean;
    private RecordStore rs;

    public Spore() {
        super();
        fld_o_boolean = false;
        disp = Display.getDisplay(this);
        mainThreadImpl = new MainThread(this);
        mainThreadImpl.aS = false;
        myThread = new Thread(mainThreadImpl);
        outInstance = this;
        String s = "/ns";
        Spore spore = this;
        InputStream inputstream = null;
        try {
            inputstream = spore.getClass().getResourceAsStream(s);
            (inputstream).read();
        }
        catch (IOException _ex) {
            if (inputstream != null) {
                try {
                    inputstream.close();
                }
                catch (IOException _ex2) {
                }
            }
        }
        finally {
            if (inputstream != null) {
                try {
                    inputstream.close();
                }
                catch (IOException _ex) {
                }
            }
        }
        // TODO Check Joys hack
//        Joys.loadJoysInfo();
        l = false;
        d = e = true;
        f = true;
        b();
    }

    private void b() {
        if (e || d) {
            if (splashScreen == null)
                splashScreen = new SplashScreen(this, mainThreadImpl, myThread, -6, -7, DeviceInfo.DEVICE_SCREEN_WIDTH, DeviceInfo.DEVICE_SCREEN_HEIGHT, "audio/amr");
            disp.setCurrent(splashScreen);
        } else {
            disp.setCurrent(mainThreadImpl);
            myThread.start();
        }
    }

    public void pauseApp() {
    }

    public void startApp() {
        if (mainThreadImpl.aS)
            mainThreadImpl.showNotify();
    }

    protected void destroyApp(boolean flag) {
        if (mainThreadImpl != null) {
            mainThreadImpl.z = false;
            mainThreadImpl = null;
        }
        myThread = null;
    }

    static void a() {
        if (mainThreadImpl != null) {
            mainThreadImpl.z = false;
            if (mainThreadImpl.recordManager != null)
                mainThreadImpl.recordManager.savePrefs();
            mainThreadImpl = null;
        }
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command == null) {
            fld_n_boolean = true;
            try {
                rs = RecordStore.openRecordStore("comfirm", true);
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                DataOutputStream dout = new DataOutputStream(bout);
                dout.writeBoolean(fld_n_boolean);
                bout.close();
                dout.close();
                byte[] data = bout.toByteArray();
                if (rs.getNextRecordID() == 1)
                    rs.addRecord(data, 0, data.length);
                else
                    rs.setRecord(1, data, 0, data.length);
                rs.closeRecordStore();
                rs = null;
            } catch (Exception ioe) {
                ioe.printStackTrace();
                System.out.println("menu.savegamerecord:" + displayable.toString());
            }
            b();
        } else {
            notifyDestroyed();
        }
    }

}